package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.FundManagerDao;
import com.youmu.sichuansfc.domain.YoumuCompany;
import com.youmu.sichuansfc.domain.YoumuFundManager;
import com.youmu.sichuansfc.domain.YoumuFundManagerPartner;
import com.youmu.sichuansfc.domain.YoumuFundManagerStaff;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.dto.fund.FundManagerDto;
import com.youmu.sichuansfc.dto.fund.FundManagerPartnerDto;
import com.youmu.sichuansfc.dto.fund.FundManagerStaffDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class FundManagerDaoImpl implements FundManagerDao {

    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public PageT findBy(Integer status, String fundName, int num, int size) {

        Map<String,Object> params = new HashMap<>();
        StringBuffer sql = new StringBuffer();
        sql.append("from YoumuFundManager where is_delete = 0 ");
        if(status != null ){
            sql.append(" and status >=:status");
            params.put("status",status);
        }
        if(fundName != null){
            sql.append(" and fundName <=:fundName");
            params.put("fundName",fundName);
        }

        sql.append(" order by add_time DESC");

        Query query =  em.createQuery(sql.toString(), YoumuFundManager.class);
        for(String key : params.keySet()){
            query.setParameter(key,params.get(key));
        }

        // 录入总共多少份档案
        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());

        List<YoumuCompany> youmuCompanies = query
                .setFirstResult(num*size)
                .setMaxResults(size)
                .getResultList();
        pageT.setPageList(youmuCompanies);

        return pageT;
    }


    public YoumuFundManager upload(YoumuFundManager youmuFundManager){
        if(youmuFundManager.getId() != null){
            youmuFundManager = em.merge(youmuFundManager);
        }else {
           em.persist(youmuFundManager);
        }
        return  youmuFundManager;
    }

    @Override
    public List<YoumuFundManager> findByFundUserId(long fundUserId) {
        String sql  = "from YoumuFundManager where is_delete = 0 and fund_user_id = :fundUserId and parent_id is null";

       return  em.createQuery(sql, YoumuFundManager.class)
                .setParameter("fundUserId", fundUserId)
                .getResultList();
    }

    @Override
    public PageT funmanagers(Integer investType, String fundManagerName, int num, int size) {

        Map<String, java.lang.Object> map = new HashMap<>();

        StringBuffer sql = new StringBuffer();
        sql.append("from YoumuFundManager where is_delete = 0  and parent_id is null \n");

        if (investType != null){
            sql.append(" and investType=:investType \n");
            map.put("investType", investType);
        }
        if (StringUtils.isNotEmpty(fundManagerName)){
            sql.append(" and managerName like:fundManagerName \n");
            map.put("fundManagerName", "%"+fundManagerName+"%");
        }
        sql.append(" order by add_time desc \n");

        PageT pageT = new PageT();
        Query query = em.createQuery(sql.toString(), YoumuFundManager.class);
        for(String key : map.keySet()){
            query.setParameter(key,map.get(key));
        }
        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num * size)
                            .setMaxResults(size)
                            .getResultList());
        return pageT;
    }

    @Override
    public FundManagerDto findById(Long id) {
        String sql  = "from YoumuFundManager where is_delete = 0 and id = :id";
        FundManagerDto fundManagerDto = new FundManagerDto();
        List<YoumuFundManager> youmuFundManagerList =  em.createQuery(sql, YoumuFundManager.class)
                .setParameter("id", id)
                .getResultList();
        if (youmuFundManagerList != null && youmuFundManagerList.size() > 0) {
            BeanUtils.copyProperties(youmuFundManagerList.get(0), fundManagerDto);
            // 公司高管
            String hql = "from YoumuFundManagerStaff where is_delete = 0 and fund_manager_id = :fundManagerId";
            List<YoumuFundManagerStaff> youmuFundManagerStaffList = em.createQuery(hql, YoumuFundManagerStaff.class)
                    .setParameter("fundManagerId",youmuFundManagerList.get(0).getId())
                    .getResultList();
            //FundManagerStaffDto fundManagerStaffDto = new FundManagerStaffDto();
            fundManagerDto.setStaffDtoList(youmuFundManagerStaffList);

            // 公司股东
            String phql = "from YoumuFundManagerPartner where is_delete = 0 and fund_manager_id = :fundManagerId";
            List<YoumuFundManagerPartner> youmuFundManagerPartnerList = em.createQuery(phql, YoumuFundManagerPartner.class)
                    .setParameter("fundManagerId",youmuFundManagerList.get(0).getId())
                    .getResultList();
            FundManagerPartnerDto FundManagerPartnerDto = new FundManagerPartnerDto();
            fundManagerDto.setPartnerList(youmuFundManagerPartnerList);
            return fundManagerDto;
        }
        return null;
    }

    @Override
    public YoumuFundManager findByName(String name) {
        String sql  = "from YoumuFundManager where is_delete = 0 and manager_name = :managerName";

        List<YoumuFundManager> youmuFundManagerList =  em.createQuery(sql, YoumuFundManager.class)
                .setParameter("managerName", name)
                .getResultList();
        if(youmuFundManagerList != null && youmuFundManagerList.size() > 0 ){
            return  youmuFundManagerList.get(0);
        }
        return null;
    }
    @Override
    public List<YoumuFundManager> findByTime(Calendar startTime, Calendar endTime) {
        String sql = "from YoumuFundManager where add_time >=:startTime and add_time <=:endTime ";
        System.out.println("startTime = " + new Timestamp(startTime.getTime().getTime())  + "  endTime = " + new Timestamp(endTime.getTime().getTime()));
        List<YoumuFundManager> youmuFundManagers = em.createQuery(sql, YoumuFundManager.class)
                .setParameter("startTime", new Timestamp(startTime.getTime().getTime()))
                .setParameter("endTime", new Timestamp(endTime.getTime().getTime()))
                .getResultList();
        return youmuFundManagers;
    }

    @Override
    public PageT findByFundUser(Long fundUserId, int num, int size) {
        Map<String, java.lang.Object> map = new HashMap<>();

        StringBuffer sql = new StringBuffer();
        sql.append("from YoumuFundManager where is_delete = 0  and fund_user_id =:fundUserId \n");

        sql.append(" order by add_time desc \n");

        Query query = em.createQuery(sql.toString(), YoumuFundManager.class);
        query.setParameter("fundUserId",fundUserId);

        PageT pageT = new PageT();

        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList());
        return pageT;
    }
}
